package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.PurchaseRecordDao;
import com.zwxbc.funplay.entity.PurchaseRecord;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PurchaseRecordDaoImpl implements PurchaseRecordDao {
	private static final String INSERT_PURCHASE_RECORD = "INSERT INTO purchase_record (player_id, game_id, purchase_date, purchase_price) VALUES (?, ?, ?, ?)";
	private static final String SELECT_PURCHASE_RECORD_BY_ID = "SELECT * FROM purchase_record WHERE purchase_id = ?";
	private static final String SELECT_ALL_PURCHASE_RECORDS = "SELECT * FROM purchase_record";
	private static final String UPDATE_PURCHASE_RECORD = "UPDATE purchase_record SET player_id = ?, game_id = ?, purchase_date = ?, purchase_price = ? WHERE purchase_id = ?";
	private static final String DELETE_PURCHASE_RECORD = "DELETE FROM purchase_record WHERE purchase_id = ?";

	@Override
	public int addPurchaseRecord(PurchaseRecord purchaseRecord) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_PURCHASE_RECORD)) {
			preparedStatement.setLong(1, purchaseRecord.getPlayerId());
			preparedStatement.setLong(2, purchaseRecord.getGameId());
			preparedStatement.setDate(3, purchaseRecord.getPurchaseDate() != null ? new java.sql.Date(purchaseRecord.getPurchaseDate().getTime()) : null);
			preparedStatement.setDouble(4, purchaseRecord.getPurchasePrice());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public PurchaseRecord getPurchaseRecordById(Long purchaseRecordId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_PURCHASE_RECORD_BY_ID)) {
			preparedStatement.setLong(1, purchaseRecordId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createPurchaseRecordFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<PurchaseRecord> getAllPurchaseRecords() {
		List<PurchaseRecord> purchaseRecords = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_PURCHASE_RECORDS);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				purchaseRecords.add(createPurchaseRecordFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return purchaseRecords;
	}

	@Override
	public int updatePurchaseRecord(PurchaseRecord purchaseRecord) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_PURCHASE_RECORD)) {
			preparedStatement.setLong(1, purchaseRecord.getPlayerId());
			preparedStatement.setLong(2, purchaseRecord.getGameId());
			preparedStatement.setDate(3, purchaseRecord.getPurchaseDate() != null ? new java.sql.Date(purchaseRecord.getPurchaseDate().getTime()) : null);
			preparedStatement.setDouble(4, purchaseRecord.getPurchasePrice());
			preparedStatement.setLong(5, purchaseRecord.getPurchaseId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deletePurchaseRecord(Long purchaseRecordId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_PURCHASE_RECORD)) {
			preparedStatement.setLong(1, purchaseRecordId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 PurchaseRecord 对象
	private PurchaseRecord createPurchaseRecordFromResultSet(ResultSet resultSet) throws SQLException {
		PurchaseRecord purchaseRecord = new PurchaseRecord();
		purchaseRecord.setPurchaseId(resultSet.getLong("purchase_id"));
		purchaseRecord.setPlayerId(resultSet.getLong("player_id"));
		purchaseRecord.setGameId(resultSet.getLong("game_id"));
		purchaseRecord.setPurchaseDate(resultSet.getDate("purchase_date"));
		purchaseRecord.setPurchasePrice(resultSet.getDouble("purchase_price"));
		return purchaseRecord;
	}
}
